angular.module(moduleName + '.directives')
  .directive('hrContact',function($anchorScroll,$location, $timeout){
    return{
      restrict:'EA',
      templateUrl:'hr-contact/hr-contact.html',
      replace:true,
      scope:{
        contact:'='
      },
      link:function(scope){
        $(function () {
          var Initials = $('.initials');
          var LetterBox=$('#letter');
          //ul下插入li
          Initials.find('ul').append('<li>A</li><li>B</li><li>C</li><li>D</li><li>E</li><li>F</li><li>G</li><li>H</li><li>I</li><li>J</li><li>K</li><li>L</li><li>M</li><li>N</li><li>O</li><li>P</li><li>Q</li><li>R</li><li>S</li><li>T</li><li>U</li><li>V</li><li>W</li><li>X</li><li>Y</li><li>Z</li><li>#</li>');
          $timeout(function(){
            initials();
          },100)
          $(".initials ul li").click(function () {
            var _this = $(this);
            var LetterHtml=_this.html();
            //选中字母淡入效果
            LetterBox.html(LetterHtml).fadeIn();
            // Initials.css('background','rgba(145,145,145,0.6)');
            //选中字母淡出效果
            setTimeout(function(){
              Initials.css('background','rgba(145,145,145,0)');
              LetterBox.fadeOut();
            },1000);
            //当前点击字母
            var letter = _this.text();
            //选中相同字母不执行$anchorScroll()方法
            // if(letter == scope.tempLetter){
            //   return;
            // }
            //获取点击字母
            scope.tempLetter = letter;

            //判断字母下是否有联系人
            if ($('#' + letter).length > 0) {
              $timeout(function () {
                $location.hash(letter);
                $anchorScroll();
              }, 100);
            }
          })
          //获取设备高度
          var windowHeight = $(window).height();
          //右侧字母高度
          var InitHeight = windowHeight - 45;
          //竖排字母最外层div高度
          Initials.height(InitHeight);
          //竖排字母间距
          var LiHeight = InitHeight / 40;
          //单个li高度
          Initials.find('li').height(LiHeight);
        })

        function initials() {//公众号排序
          var SortList = $(".sort_list");
          var SortBox = $(".sort_box");
          SortList.sort(asc_sort).appendTo('.sort_box');//按首字母排序
          function asc_sort(a, b) {
            return makePy($(b).find('.num_name').text().charAt(0))[0].toUpperCase() < makePy($(a).find('.num_name').text().charAt(0))[0].toUpperCase() ? 1 : -1;
          }

          var initials = [];
          var num = 0;
          SortList.each(function (i) {
            var initial = makePy($(this).find('.num_name').text().charAt(0))[0].toUpperCase();
            if (initial >= 'A' && initial <= 'Z') {
              if (initials.indexOf(initial) === -1)
                initials.push(initial);
            } else {
              num++;
            }

          });
          $.each(initials, function (index, value) {//添加首字母标签
            SortBox.append('<div class="sort_letter" id="' + value + '">' + value + '</div>');
          });
          if (num != 0) {
            SortBox.append('<div class="sort_letter" id="default">#</div>');
          }

          for (var i = 0; i < SortList.length; i++) {//插入到对应的首字母后面
            var letter = makePy(SortList.eq(i).find('.num_name').text().charAt(0))[0].toUpperCase();
            switch (letter) {
              case "A":
                $('#A').after(SortList.eq(i));
                break;
              case "B":
                $('#B').after(SortList.eq(i));
                break;
              case "C":
                $('#C').after(SortList.eq(i));
                break;
              case "D":
                $('#D').after(SortList.eq(i));
                break;
              case "E":
                $('#E').after(SortList.eq(i));
                break;
              case "F":
                $('#F').after(SortList.eq(i));
                break;
              case "G":
                $('#G').after(SortList.eq(i));
                break;
              case "H":
                $('#H').after(SortList.eq(i));
                break;
              case "I":
                $('#I').after(SortList.eq(i));
                break;
              case "J":
                $('#J').after(SortList.eq(i));
                break;
              case "K":
                $('#K').after(SortList.eq(i));
                break;
              case "L":
                $('#L').after(SortList.eq(i));
                break;
              case "M":
                $('#M').after(SortList.eq(i));
                break;
              case "O":
                $('#O').after(SortList.eq(i));
                break;
              case "P":
                $('#P').after(SortList.eq(i));
                break;
              case "Q":
                $('#Q').after(SortList.eq(i));
                break;
              case "R":
                $('#R').after(SortList.eq(i));
                break;
              case "S":
                $('#S').after(SortList.eq(i));
                break;
              case "T":
                $('#T').after(SortList.eq(i));
                break;
              case "U":
                $('#U').after(SortList.eq(i));
                break;
              case "V":
                $('#V').after(SortList.eq(i));
                break;
              case "W":
                $('#W').after(SortList.eq(i));
                break;
              case "X":
                $('#X').after(SortList.eq(i));
                break;
              case "Y":
                $('#Y').after(SortList.eq(i));
                break;
              case "Z":
                $('#Z').after(SortList.eq(i));
                break;
              default:
                $('#default').after(SortList.eq(i));
                break;
            }
          };
        }
      }
    };
  })